package com.budwk.app.sqfwV2.generator.childrenVisitation.pdf;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.budwk.app.sqfw.constants.CommonConstants;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CommonParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwV2.component.ProblemComponent;
import com.budwk.app.sqfwV2.component.SignImageComponent;
import com.budwk.app.sqfwV2.dto.dto.childrenVisits.ChildrenVisitationPdfDTO;
import com.budwk.app.sqfwV2.dto.dto.childrenVisits.GuardianInfo;
import com.budwk.app.sqfwV2.enums.FileServiceConfigEnum;
import com.budwk.app.util.ClassUtil;
import com.budwk.app.util.StrUtil;
import com.deepoove.poi.policy.RenderPolicy;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 儿童探访记录pdf
 * @since 2024-04-23
 *
 * 白云区儿童探访记录 ChildrenVisitation
 * 白云区儿童探访记录表.docx
 */
public class ChildrenVisitsWord extends BasePdfGenerator{

    /**
    * 判断使用使用这个generator生成pdf
    *
    * @param pdfDTO 创建pdf数据素材
    * @return 结果
    */
    @Override
    public boolean pdfCreateConditionCheck(PdfGenerateParam pdfDTO) {
        ChildrenVisitationPdfDTO fieldDTO = (ChildrenVisitationPdfDTO) pdfDTO.getAbstractCustomerPdfCreateField();
        return true;
    }

    @Override
    public String getTemplatePath(PdfGenerateParam pdfGenerateParam) {
        return FileServiceConfigEnum.CA.getFilePath();
    }

    @Override
    public void customerPopularPdfParam(Map<String, Object> paramMap, PdfGenerateParam pdfGenerateParam) {
        if (pdfGenerateParam.isDownTemplate()) {
            buildNullData(paramMap);
            return;
        }
        // 在这里对一些数据进行填充 进行复杂的业务代码逻辑
        ChildrenVisitationPdfDTO fieldDTO = (ChildrenVisitationPdfDTO) pdfGenerateParam.getAbstractCustomerPdfCreateField();
        // 儿童类别
        paramMap.put("childrenType", fieldDTO.getChildrenType());
        paramMap.put("otherCategory", StrUtil.formatStr(fieldDTO.getOtherCategory()));
        // 探访时间
        paramMap.put("fillDate", LocalDateTimeUtil.format(fieldDTO.getFillDate(), DatePattern.CHINESE_DATE_PATTERN));
        String householdAddress = CommonConstants.TRUE.equals(fieldDTO.getIsLiveRegisteredAddress()) ? fieldDTO.getAddress() : fieldDTO.getHouseholdAddress();
        paramMap.put("householdAddress", StrUtil.formatStr(householdAddress));
        paramMap.put("visitedName", StrUtil.formatStr(fieldDTO.getVisitedName()));
        paramMap.put("citizenSex", StrUtil.formatStr(fieldDTO.getCitizenSex()));
        paramMap.put("birthday", fieldDTO.getBirthday());

        paramMap.put("idcard", StrUtil.formatStr(fieldDTO.getIdcard()));
        paramMap.put("promotionSummary", StrUtil.replaceNullIsNo(fieldDTO.getPromotionSummary()));
        paramMap.put("unlearnedCondition", StrUtil.formatStr(fieldDTO.getUnlearnedCondition()));
        // 家庭成员
        paramMap.put("guardianInfos", getGuardianInfo(fieldDTO));

        CommonParam commonParam = pdfGenerateParam.getCreateUserCommonParam();
        paramMap.put("district", StrUtil.formatZhiShu(commonParam.getDistrict()));
        // 探访人员签名m
        SignImageComponent.signWord(paramMap, null, commonParam.getCreateUserSigUrl(), commonParam.getCreateUserName(), "checkedUnitLeader", "checkedUnitLeaderStr");
        // 图片
        SignImageComponent.signWord(paramMap, 7.0F, fieldDTO.getInspectionPhotos(), "无", "inspectionPhotos", "inspectionPhotosStr");

    }

    protected Map<String, RenderPolicy> createCustomerRenderPolicy(Map<String, Object> paramMap, PdfGenerateParam pdfGenerateParam) {
        return ProblemComponent.createCustomerRenderPolicy("guardianInfos");
    }

    private List<Map<String, Object>> getGuardianInfo(ChildrenVisitationPdfDTO fieldDTO) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        List<GuardianInfo> guardianInfos = fieldDTO.getGuardianInfos();
        if (CollectionUtil.isNotEmpty(guardianInfos)) {
            for (GuardianInfo guardianInfo : guardianInfos) {
                Map<String, Object> map = getMap(guardianInfo);
                mapList.add(map);
            }
        }
        return mapList;
    }



    private static Map<String, Object> getMap(GuardianInfo guardianInfo) {
        Map<String, Object> map = new HashMap<>();
        map.put(ClassUtil.getField(GuardianInfo::getName), guardianInfo.getName());
        map.put(ClassUtil.getField(GuardianInfo::getPhone), guardianInfo.getPhone());
        map.put(ClassUtil.getField(GuardianInfo::getRelationWithChildren), guardianInfo.getRelationWithChildren());
        map.put(ClassUtil.getField(GuardianInfo::getWorkSituation), guardianInfo.getWorkSituation());
        return map;
    }

    private void buildNullData(Map<String, Object> paramMap) {
        paramMap.put("childrenType", "xx");
        paramMap.put("otherCategory", "");
        paramMap.put("fillDate", LocalDateTimeUtil.format(LocalDateTime.now(), DatePattern.CHINESE_DATE_PATTERN));
        paramMap.put("householdAddress", "");
        paramMap.put("visitedName", "");
        paramMap.put("citizenSex", "");
        paramMap.put("birthday", "");
        paramMap.put("idcard", "");
        paramMap.put("promotionSummary", "");
        paramMap.put("unlearnedCondition", "");
        paramMap.put("guardianInfos", new ArrayList<>());
        paramMap.put("district", "");
        paramMap.put("checkedUnitLeader", null);
        paramMap.put("checkedUnitLeaderStr", "");
        paramMap.put("inspectionPhotos", null);
        paramMap.put("inspectionPhotosStr", "");
    }
}
